package com.mowanka.mokeng.ui.fragment.trading

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.kingja.loadsir.core.LoadService
import com.mowanka.mokeng.app.base.BaseFragment
import com.mowanka.mokeng.app.ext.*
import com.mowanka.mokeng.app.weight.recyclerview.layoutmanager.FullyStaggeredGridLayoutManager
import com.mowanka.mokeng.databinding.FragmentTradingTransSecondhandBinding
import com.mowanka.mokeng.ui.adapter.CommentProductAdapter
import com.mowanka.mokeng.viewmodel.request.RequestTradingViewModel
import com.zbc.mwkdialog.MenuDialog
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class TransSecondHandFragment(private val type: Int) : BaseFragment<BaseViewModel, FragmentTradingTransSecondhandBinding>() {

    private val requestMap by lazy {
        mutableMapOf<String, Any>().apply {
            this["filterType"] = if (type == 1) 3 else 4
        }
    }

    //适配器
    private val productAdapter by lazy { CommentProductAdapter(arrayListOf()) }

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    private val requestTradingViewModel by viewModels<RequestTradingViewModel>()

    override fun initView(savedInstanceState: Bundle?) {
        //状态页配置
        loadsir = loadServiceInit(mDatabind.includeRecyclerview.swipeRefresh) {
            loadsir.showLoading()
            requestTradingViewModel.getProductList(requestMap)
        }
        //初始化recyclerView
        mDatabind.includeRecyclerview.recyclerView.init(
            FullyStaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL),
            productAdapter
        ).let {
            it.itemAnimator = null
            it.setHasFixedSize(true)
            it.setPadding(mActivity.pt2px(3.5f), 0, mActivity.pt2px(3.5f), 0)
            it.initFooter { requestTradingViewModel.getProductList(requestMap, false) }
            //初始化FloatingActionButton
//            it.initFloatBtn(mDatabind.floatBtn)
        }
        //初始化 SwipeRefreshLayout
        mDatabind.includeRecyclerview.swipeRefresh.isEnabled = false
        productAdapter.run {
            setOnItemClickListener { _, _, position ->
                MenuDialog.Builder(activity)
                    .setList(mutableListOf<String>().apply {
                        add("不允许")
                        add("允许")
                    })
                    .setListener { _, pos, _ ->
//                        requestMeInfoViewModel.updatePrivacy(privacyAdapter.data[position].id, pos)
                    }
                    .show()
            }
        }
    }

    override fun lazyLoadData() {
        loadsir.showLoading()
        requestTradingViewModel.getProductList(requestMap)
    }

    override fun createObserver() {
        requestTradingViewModel.productList.observe(this) {
            loadListData(
                it,
                productAdapter,
                loadsir,
                mDatabind.includeRecyclerview.recyclerView,
                mDatabind.includeRecyclerview.swipeRefresh
            )
        }
    }
}